//
//  JWBorderTextField.m
//  LiveReception
//
//  Created by sunjiawen on 2017/9/11.
//  Copyright © 2017年 zoosnet. All rights reserved.
//

#import "JWBorderTextField.h"

@implementation JWBorderTextField
{
    CALayer *borderBottomLayer;
}
@dynamic borderWhich;

- (void)awakeFromNib {
    
    [super awakeFromNib];

}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    self.highLight = NO;
    
    [self addTarget:self action:@selector(editingDidBegin) forControlEvents:UIControlEventEditingDidBegin];
    [self addTarget:self action:@selector(editingDidEnd) forControlEvents:UIControlEventEditingDidEnd];
    
}


- (void)setTborderWhich:(int)tborderWhich {
    switch (tborderWhich) {
        case 1:
            self.borderWhich = JWViewBorderTop;
            break;
        case 2:
            self.borderWhich = JWViewBorderLeft;
            break;
        case 3:
            self.borderWhich = JWViewBorderBottom;
            break;
        case 4:
            self.borderWhich = JWViewBorderRight;
            break;
        default:
            return;
            break;
    }
}

/**
 *  设置边框颜色
 *
 *  @param borderColor 可视化视图传入的值
 */
- (void)setBorderColor:(UIColor *)borderColor {
    
    self.layer.borderColor = borderColor.CGColor;
}


- (void)editingDidBegin {
    self.highLight = YES;
}

- (void)editingDidEnd {
    self.highLight = NO;
}

- (void)setBorderWhich:(JWViewBorder)borderWhich {
    self.layer.borderWidth = 0;
    CGFloat bh = 0.3;
    
    if (borderWhich & JWViewBorderBottom) {
        [self addBottomBorder:self borderHeight:bh];
    }
    if (borderWhich & JWViewBorderLeft) {
        [self addLeftBorder:self borderHeight:bh];
    }
    if (borderWhich & JWViewBorderRight) {
        [self addRightBorder:self borderHeight:bh];
    }
    if (borderWhich & JWViewBorderTop) {
        [self addTopBorder:self borderHeight:bh];
    }

}

- (void)addTopBorder:(UIView *)vi borderHeight:(CGFloat)bh {
    CGColorRef col = vi.layer.borderColor;
    CALayer *border = [CALayer layer];
    border.frame = CGRectMake(0, 0, vi.frame.size.width, bh);
    border.backgroundColor = col;
    [vi.layer addSublayer:border];
}

- (void)addLeftBorder:(UIView *)vi borderHeight:(CGFloat)bh{
    CGColorRef col = vi.layer.borderColor;

    CALayer *border = [CALayer layer];
    border.frame = CGRectMake(0, 0, bh, vi.frame.size.height);
    border.backgroundColor = col;
    [vi.layer addSublayer:border];
}

- (void)addBottomBorder:(UIView *)vi borderHeight:(CGFloat)bh{
    CGColorRef col = vi.layer.borderColor;
    CALayer *border = [CALayer layer];
    border.frame = CGRectMake(0, vi.frame.size.height-bh, vi.frame.size.width, bh);
    border.backgroundColor = col;
    [vi.layer addSublayer:border];
    
    borderBottomLayer = border;
}

- (void)addRightBorder:(UIView *)vi borderHeight:(CGFloat)bh{
    CGColorRef col = vi.layer.borderColor;
    CALayer *border = [CALayer layer];
    border.frame = CGRectMake(vi.frame.size.width-bh, 0, bh, vi.frame.size.height);
    border.backgroundColor = col;
    [vi.layer addSublayer:border];
}

- (void)setHighLight:(BOOL)highLight {
    _highLight = highLight;
    
    if (_highLight == YES) {
        borderBottomLayer.backgroundColor = [UIColor whiteColor].CGColor;
    }else {
        borderBottomLayer.backgroundColor = self.layer.borderColor;
    }
}
@end
